package com.hw.one.dam2.service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.hw.one.dam2.dao.IPollutionDao;
import com.hw.one.dam2.param.PollutionParam;
import com.hw.one.dam2.util.WeatherUtil;
import com.hw.one.dam2.util.UserSiteUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hw.one.core.result.MsgResult;
import com.hw.one.dam2.common.DamConstant;
import com.hw.one.dam2.common.DataTypeEnum;
import com.hw.one.dam2.common.SiteTypeEnum;
import com.hw.one.dam2.dao.IIndustryPollutionDao;
import com.hw.one.dam2.dao.IRegionSiteDao;
import com.hw.one.dam2.dao.ISmallAirDao;
import com.hw.one.dam2.entity.PollutionIndustryEntity;
import com.hw.one.dam2.entity.RaiseDustEntity;
import com.hw.one.dam2.param.PollutionIndustryParam;
import com.hw.one.dam2.param.RegionSiteParam;
import com.hw.one.dam2.param.SmallAirParam;
import com.hw.one.dam2.util.CalendarUtil;
import com.hw.one.ext.util.UserUtil;
import com.hw.one.util.DateTimeUtil;

@Service
public class IndustryPollutionService implements IIndustryPollutionService {

    @Autowired
    private IIndustryPollutionDao industryPollutionDao;

    @Autowired
    private IRegionSiteDao regionSiteDao;

    @Autowired
    private ISmallAirDao smallAirDao;

    // 污染源
    @Autowired
    private IPollutionDao pollutionDao;

    // 数字保留两位小数
    DecimalFormat decimalformat = new DecimalFormat("#0.00");

    /**
     * @throws Exception
     */
    @Override
    public MsgResult findIndustryPollutionBySiteCodeAndTime(HttpServletRequest request) throws Exception {
        // 判断用户是否具有访问权限
        if (!UserSiteUtil.userIsNotContainSiteTypes("101")) {
            return MsgResult.okResult(new ArrayList<PollutionIndustryEntity>());
        }

        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");

        Calendar calendar = Calendar.getInstance();

        if (null == endTime || "".equals(endTime)) {
            endTime = DateTimeUtil.getFormatDateStr(calendar.getTime(), "yyyy-MM-dd HH:00:00");
        }

        if (null == startTime || "".equals(startTime)) {
            calendar.add(Calendar.DAY_OF_MONTH, -2);
            startTime = DateTimeUtil.getFormatDateStr(calendar.getTime(), "yyyy-MM-dd HH:00:00");
        }
        // 数据类型：小时数据hourData;日数据 dayData
        String dataType = request.getParameter("dataType");
        dataType = null == dataType || "".equals(dataType) ? "hourData" : dataType;

        Map<String, Object> region = UserUtil.getRegion();
        String userRegionCode = (String) region.get("regionCode");
        //String userRegionType = (String) region.get("regionType");

        /**
         * 根据当前登录用户区域，查询该区域的上级区域
         *
         * 如果是区（regionType为22）县（regionType为24）需要查找上级区域，直属市
         *
         */
        /*if (20 <= Integer.parseInt(userRegionType)) {
            List<Map<String, Object>> parentRegionList = regionSiteDao.findParentRegionInfoByRegionCode(userRegionCode);
			// 把登录用户所属省份、和市编码，读取出来
			for (Map<String, Object> parentRegionMap : parentRegionList) {
				String retionType = String.valueOf(parentRegionMap.get("regionType"));
				// 市级名称
				if ("20".equals(retionType)) {
					userRegionCode =  String.valueOf(parentRegionMap.get("regionCode"));
					break;
				}
			}
		}*/

        // 查询点位信息的参数Map
        RegionSiteParam paraSiteMap = new RegionSiteParam();
        paraSiteMap.setRegionCode(userRegionCode);
        paraSiteMap.setSiteType(SiteTypeEnum.INDUSTRY_SITE.getValue());
        // 查询城市和企业名称
        String regionCode = request.getParameter("regionCode");
        String companyName = request.getParameter("companyName");
        String siteCodes = request.getParameter("siteCodes");
        if (null != siteCodes && !"".equals(siteCodes)) {
            List<String> siteCodeList = new ArrayList<>();
            Collections.addAll(siteCodeList, siteCodes.split(","));
            paraSiteMap.setSiteCodeList(siteCodeList);
        }
        paraSiteMap.setCompanyName(companyName);
        if (null != regionCode) {
            paraSiteMap.setRegionCode(regionCode);
        }

        // 点位信息列表
        List<Map<String, Object>> siteInfoList = regionSiteDao.findSiteInfoByRegionCodeAndSiteType(paraSiteMap);

        if (siteInfoList == null || siteInfoList.size() == 0) {
            return MsgResult.okResult(siteInfoList);
        }
        // 点位编码列
        List<String> pointCodeList = new ArrayList<String>();
        for (Map<String, Object> siteMap : siteInfoList) {
            pointCodeList.add(String.valueOf(siteMap.get("siteCode")));
        }

        // 查询工业源 排口信息
        List<Map<String, Object>> outputInfoList = regionSiteDao.findIndustyOutputNameBySiteCode(pointCodeList);
        // 排口编号列表
        List<String> outputCodeList = new ArrayList<String>();
        for (Map<String, Object> outputMap : outputInfoList) {
            outputCodeList.add(String.valueOf(outputMap.get("outputCode")));
        }

        // 拼凑 查询工业污染源的数据
        PollutionIndustryParam paramPO = new PollutionIndustryParam();
        paramPO.setPSCodeList(pointCodeList);
        paramPO.setOutputCodeList(outputCodeList);
        paramPO.setStartTime(startTime);
        paramPO.setEndTime(endTime);

        List<PollutionIndustryEntity> industryPollutionList = null;
        // 小时数据
        if (dataType.equals("hourData")) {
            industryPollutionList = industryPollutionDao.findIndustryPollutionBySiteCodeAndTime(paramPO,
                "T_DATA_GASPOLLUTION_HOUR_S_COLLECT");
            // 把时间格式化一下
            for (PollutionIndustryEntity tempPO : industryPollutionList) {
                String curMT = tempPO.getMonitorTime();
                if (null != curMT && !"".equals(curMT)) {
                    tempPO.setMonitorTime(curMT.substring(0, 16));
                }
            }
        } else {
            // 日数据
            industryPollutionList = industryPollutionDao.findIndustryPollutionBySiteCodeAndTime(paramPO,
                "T_DATA_GASPOLLUTION_DAY_S_COLLECT");
            // 把时间格式化一下
            for (PollutionIndustryEntity tempPO : industryPollutionList) {
                String curMT = tempPO.getMonitorTime();
                if (null != curMT && !"".equals(curMT)) {
                    tempPO.setMonitorTime(curMT.substring(0, 10));
                }
            }
        }

        List<PollutionIndustryEntity> returnList = new ArrayList<PollutionIndustryEntity>();
        if (null != industryPollutionList) {

            int sortNum = 1;

            for (Map<String, Object> siteMap : siteInfoList) {
                String siteCode = (String) siteMap.get("siteCode");
                String monitorTime = null;
                Double AvgStrength001 = 0.0;
                Double AvgStrength002 = 0.0;
                Double AvgStrength003 = 0.0;

                Double AvgZsStrength001 = 0.0;
                Double AvgZsStrength002 = 0.0;
                Double AvgZsStrength003 = 0.0;

                Double PFL001 = 0.0;
                Double PFL002 = 0.0;
                Double PFL003 = 0.0;

                Double AvgFlow = 0.0;
                Double CouFlow = 0.0;
                int avgNum = 0;

                String siteName = null;
                String cityName = null;

                PollutionIndustryEntity newDataPO = null;
                for (PollutionIndustryEntity ipPo : industryPollutionList) {
                    if (siteCode.equals(ipPo.getPSCode())) {
                        avgNum = avgNum + 1;

                        siteName = String.valueOf(siteMap.get("siteName"));
                        cityName = String.valueOf(siteMap.get("RegionName"));

                        monitorTime = ipPo.getMonitorTime();
                        String AvgStrength001Obj = ipPo.getAvgStrength001();
                        AvgStrength001Obj = null == AvgStrength001Obj || "".equals(AvgStrength001Obj) ? "0.0"
                            : AvgStrength001Obj;
                        AvgStrength001 = AvgStrength001 + Double.valueOf(AvgStrength001Obj);

                        String AvgStrength002Obj = ipPo.getAvgStrength002();
                        AvgStrength002Obj = null == AvgStrength002Obj || "".equals(AvgStrength002Obj) ? "0.0"
                            : AvgStrength002Obj;
                        AvgStrength002 = AvgStrength002 + Double.valueOf(AvgStrength002Obj);

                        String AvgStrength003Obj = ipPo.getAvgStrength003();
                        AvgStrength003Obj = null == AvgStrength003Obj || "".equals(AvgStrength003Obj) ? "0.0"
                            : AvgStrength003Obj;
                        AvgStrength003 = AvgStrength003 + Double.valueOf(AvgStrength003Obj);

                        String AvgZsStrength001Obj = ipPo.getAvgZsStrength001();
                        AvgZsStrength001Obj = null == AvgZsStrength001Obj || "".equals(AvgZsStrength001Obj) ? "0.0"
                            : AvgZsStrength001Obj;
                        AvgZsStrength001 = AvgZsStrength001 + Double.valueOf(AvgZsStrength001Obj);

                        String AvgZsStrength002Obj = ipPo.getAvgZsStrength002();
                        AvgZsStrength002Obj = null == AvgZsStrength002Obj || "".equals(AvgZsStrength002Obj) ? "0.0"
                            : AvgZsStrength002Obj;
                        AvgZsStrength002 = AvgZsStrength002 + Double.valueOf(AvgZsStrength002Obj);

                        String AvgZsStrength003Obj = ipPo.getAvgZsStrength003();
                        AvgZsStrength003Obj = null == AvgZsStrength003Obj || "".equals(AvgZsStrength003Obj) ? "0.0"
                            : AvgZsStrength003Obj;
                        AvgZsStrength003 = AvgZsStrength003 + Double.valueOf(AvgZsStrength003Obj);

                        String PFL001Obj = ipPo.getPFL001();
                        PFL001Obj = null == PFL001Obj || "".equals(PFL001Obj) ? "0.0" : PFL001Obj;
                        PFL001 = PFL001 + Double.valueOf(PFL001Obj);

                        String PFL002Obj = ipPo.getPFL002();
                        PFL002Obj = null == PFL002Obj || "".equals(PFL002Obj) ? "0.0" : PFL002Obj;
                        PFL002 = PFL002 + Double.valueOf(PFL002Obj);

                        String PFL003Obj = ipPo.getPFL003();
                        PFL003Obj = null == PFL003Obj || "".equals(PFL003Obj) ? "0.0" : PFL003Obj;
                        PFL003 = PFL003 + Double.valueOf(PFL003Obj);

                        String AvgFlowObj = ipPo.getAvgFlow();
                        AvgFlowObj = null == AvgFlowObj || "".equals(AvgFlowObj) ? "0.0" : AvgFlowObj;
                        AvgFlow = AvgFlow + Double.valueOf(AvgFlowObj);

                        String CouFlowObj = ipPo.getCouFlow();
                        CouFlowObj = null == CouFlowObj || "".equals(CouFlowObj) ? "0.0" : CouFlowObj;
                        CouFlow = CouFlow + Double.valueOf(CouFlowObj);
                    } // end if
                }

                if (avgNum > 0) {
                    AvgStrength001 = AvgStrength001 / avgNum;
                    AvgStrength002 = AvgStrength002 / avgNum;
                    AvgStrength003 = AvgStrength003 / avgNum;

                    AvgZsStrength001 = AvgZsStrength001 / avgNum;
                    AvgZsStrength002 = AvgZsStrength002 / avgNum;
                    AvgZsStrength003 = AvgZsStrength003 / avgNum;

                    PFL001 = PFL001 / avgNum;
                    PFL002 = PFL002 / avgNum;
                    PFL003 = PFL003 / avgNum;

                    newDataPO = new PollutionIndustryEntity();
                    newDataPO.setIndex(String.valueOf(sortNum++));
                    newDataPO.setPSCode(siteCode);
                    newDataPO.setCompanyName(siteName);
                    newDataPO.setCityName(cityName);
                    newDataPO.setMonitorTime(monitorTime);
                    newDataPO.setAvgStrength001(decimalformat.format(AvgStrength001));
                    newDataPO.setAvgStrength002(decimalformat.format(AvgStrength002));
                    newDataPO.setAvgStrength003(decimalformat.format(AvgStrength003));

                    newDataPO.setAvgZsStrength001(decimalformat.format(AvgZsStrength001));
                    newDataPO.setAvgZsStrength002(decimalformat.format(AvgZsStrength002));
                    newDataPO.setAvgZsStrength003(decimalformat.format(AvgZsStrength003));

                    newDataPO.setPFL001(decimalformat.format(PFL001));
                    newDataPO.setPFL002(decimalformat.format(PFL002));
                    newDataPO.setPFL003(decimalformat.format(PFL003));

                    newDataPO.setAvgFlow(decimalformat.format(AvgFlow));
                    newDataPO.setCouFlow(decimalformat.format(CouFlow));

                    returnList.add(newDataPO);
                }
            }
        }

        return MsgResult.okResult(returnList);
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    public Object findPollutionTrendDataBySiteCodeAndSiteType(Map<String, Object> paraMap) {

        // 工业源 industrySite 扬尘 raiseDustSite 油烟 lampblackSite 喷漆房 sprayBoothSite
        String pointType = (String) paraMap.get("pointType");
        String pointCode = (String) paraMap.get("pointCode");
        String dataType = (String) paraMap.get("dataType");
        String startTime = (String) paraMap.get("startTime");
        String endTime = (String) paraMap.get("endTime");
        List<String> pointList = new ArrayList<>();
        pointList.add(pointCode);
        JSONObject detailInfoObject = null;
        if (SiteTypeEnum.INDUSTRY_SITE.getValue().equals(pointType)) {
            PollutionIndustryParam param = new PollutionIndustryParam();
            param.setStartTime(startTime);
            param.setEndTime(endTime);
            param.setPSCodeList(pointList);
            List<Map> resultList = null;
            // 查询工业源 排口信息
            List<Map<String, Object>> outputInfoList = regionSiteDao.findIndustyOutputNameBySiteCode(pointList);
            // 排口编号列表
            List<String> outputCodeList = new ArrayList<String>();
            for (Map<String, Object> outputMap : outputInfoList) {
                outputCodeList.add(String.valueOf(outputMap.get("outputCode")));
            }
            param.setOutputCodeList(outputCodeList);
            List<String> monitorTimeList = null;
            // 判断小时数据还是日数据
            if (DataTypeEnum.HOUR_DATA.getValue().equals(dataType)) {
                resultList = industryPollutionDao.findIndustryPollutionBySiteCodeAndSDTime(param,
                    "T_DATA_GASPOLLUTION_HOUR_S_COLLECT");
                // 把时间格式化一下
                for (Map tempMap : resultList) {
                    Object curMT = tempMap.get("MONITORTIME");
                    if (null != curMT && !"".equals(curMT)) {
                        tempMap.put("MONITORTIME", String.valueOf(curMT).substring(0, 16));
                    }
                }
                monitorTimeList = CalendarUtil.getDatesBetweenTwoDate(startTime.substring(0, 16), endTime.substring(0, 16), "hour");
            } else if (DataTypeEnum.DAY_DATA.getValue().equals(dataType)) {
                resultList = industryPollutionDao.findIndustryPollutionBySiteCodeAndSDTime(param,
                    "T_DATA_GASPOLLUTION_DAY_S_COLLECT");
                // 把时间格式化一下
                for (Map tempMap : resultList) {
                    Object curMT = tempMap.get("MONITORTIME");
                    if (null != curMT && !"".equals(curMT)) {
                        tempMap.put("MONITORTIME", String.valueOf(curMT).substring(0, 10));
                    }
                }
                monitorTimeList = CalendarUtil.getDatesBetweenTwoDate(startTime.substring(0, 10), endTime.substring(0, 10), "day");
            }

//			List<String> monitorTimeList = new ArrayList<>();


//			// 遍历时间,并组装时间list
//			String curTime = "";
//			for (Map map : resultList) {
//				Object monitorTime = map.get("MONITORTIME");
//				if (!curTime.equals(monitorTime)) {
//					curTime = monitorTime.toString();
//					monitorTimeList.add(curTime);
//				}
//			}
            // 创建对象
            List<String> AvgStrength001List = new ArrayList<String>();
            List<String> AvgStrength002List = new ArrayList<String>();
            List<String> AvgStrength003List = new ArrayList<String>();
            // 循环时间list, 并把相同时间的数据相加取平均值
            for (String timeStr : monitorTimeList) {
                Double AvgStrength001 = 0.0;
                Double AvgStrength002 = 0.0;
                Double AvgStrength003 = 0.0;

                int index = 0;
                for (Map siteDataMap : resultList) {
                    Object monitorTime = siteDataMap.get("MONITORTIME");
                    if (timeStr.equals(monitorTime)) {
                        index = index + 1;
                        Object AvgStrength001Obj = siteDataMap.get("AvgStrength001");
                        AvgStrength001Obj = null == AvgStrength001Obj || "".equals(AvgStrength001Obj) ? "0.0"
                            : AvgStrength001Obj;
                        AvgStrength001 = AvgStrength001 + Double.valueOf(AvgStrength001Obj.toString());

                        Object AvgStrength002Obj = siteDataMap.get("AvgStrength002");
                        AvgStrength002Obj = null == AvgStrength002Obj || "".equals(AvgStrength002Obj) ? "0.0"
                            : AvgStrength002Obj;
                        AvgStrength002 = AvgStrength002 + Double.valueOf(AvgStrength002Obj.toString());

                        Object AvgStrength003Obj = siteDataMap.get("AvgStrength003");
                        AvgStrength003Obj = null == AvgStrength003Obj || "".equals(AvgStrength003Obj) ? "0.0"
                            : AvgStrength003Obj;
                        AvgStrength003 = AvgStrength003 + Double.valueOf(AvgStrength003Obj.toString());
                    }
                }
                if (index > 0) {
                    AvgStrength001 = AvgStrength001 / index;
                    AvgStrength002 = AvgStrength002 / index;
                    AvgStrength003 = AvgStrength003 / index;
                    AvgStrength001List.add(decimalformat.format(AvgStrength001));
                    AvgStrength002List.add(decimalformat.format(AvgStrength002));
                    AvgStrength003List.add(decimalformat.format(AvgStrength003));
                } else {
                    AvgStrength001List.add("-");
                    AvgStrength002List.add("-");
                    AvgStrength003List.add("-");
                }

            }
            List<String> pollutionFactorList = new ArrayList<String>();
            pollutionFactorList.add("烟尘");
            pollutionFactorList.add("二氧化硫");
            pollutionFactorList.add("氮氧化物");

            JSONArray dataArr = new JSONArray();
            detailInfoObject = new JSONObject();
            dataArr.add(AvgStrength001List);
            dataArr.add(AvgStrength002List);
            dataArr.add(AvgStrength003List);
            detailInfoObject.put("dataList", dataArr);
            detailInfoObject.put("pollutionFactor", pollutionFactorList);
            detailInfoObject.put("time", monitorTimeList);
        } else if (SiteTypeEnum.RAISE_DUST_SITE.getValue().equals(pointType)) {
            SmallAirParam smallAirParam = new SmallAirParam();
            smallAirParam.setStartTime(startTime);
            smallAirParam.setEndTime(endTime);
            smallAirParam.setSiteCodes(pointList);

            List<String> monitorTimeList = null;
            List<Map> raiseDustDataList = null;
            if (DataTypeEnum.HOUR_DATA.getValue().equals(dataType)) {
                raiseDustDataList = smallAirDao.findSmallHourDataByPointCodeAndTime(smallAirParam);
                Collections.reverse(raiseDustDataList);
                // 把时间格式化一下
                for (Map tempMap : raiseDustDataList) {
                    Object curMT = tempMap.get("MONITORTIME");
                    if (null != curMT && !"".equals(curMT)) {
                        tempMap.put("MONITORTIME", String.valueOf(curMT).substring(0, 16));
                    }
                }
                monitorTimeList = CalendarUtil.getDatesBetweenTwoDate(startTime.substring(0, 16), endTime.substring(0, 16), "hour");

            } else if (DataTypeEnum.DAY_DATA.getValue().equals(dataType)) {
                raiseDustDataList = smallAirDao.findSmallDayDataByPointCodeAndTime(smallAirParam);
                Collections.reverse(raiseDustDataList);
                // 把时间格式化一下
                for (Map tempMap : raiseDustDataList) {
                    Object curMT = tempMap.get("MONITORTIME");
                    if (null != curMT && !"".equals(curMT)) {
                        tempMap.put("MONITORTIME", String.valueOf(curMT).substring(0, 10));
                    }
                }
                monitorTimeList = CalendarUtil.getDatesBetweenTwoDate(startTime.substring(0, 10), endTime.substring(0, 10), "day");
            }
            JSONArray raiseDustNameList = new JSONArray();
            JSONArray PM10Arr = new JSONArray();
            JSONArray PM25Arr = new JSONArray();
            for (String monitorTime : monitorTimeList) {
                boolean flag = false;
                for (Map raiseDust : raiseDustDataList) {
                    if (monitorTime.equals(raiseDust.get("MONITORTIME"))) {
                        flag = true;
                        PM10Arr.add(raiseDust.get("PM10"));
                        PM25Arr.add(raiseDust.get("PM25"));
                        break;
                    }
                }
                if (!flag) {
                    PM10Arr.add("-");
                    PM25Arr.add("-");
                }

            }
            JSONArray dataList = new JSONArray();
            dataList.add(PM10Arr);
            dataList.add(PM25Arr);
            raiseDustNameList.add("PM10");
            raiseDustNameList.add("PM2.5");
            detailInfoObject = new JSONObject();
            detailInfoObject.put("dataList", dataList);
            detailInfoObject.put("pollutionFactor", raiseDustNameList);
            detailInfoObject.put("time", monitorTimeList);
        } else if (SiteTypeEnum.LAMPBLACK_SITE.getValue().equals(pointType)) {
            SmallAirParam smallAirParam = new SmallAirParam();
            smallAirParam.setStartTime(startTime);
            smallAirParam.setEndTime(endTime);
            smallAirParam.setSiteCodes(pointList);

            List<String> monitorTimeList = null;
            List<Map> lampBlackList = null;
            if (DataTypeEnum.HOUR_DATA.getValue().equals(dataType)) {
                // 小时数据
                lampBlackList = smallAirDao.findSmallHourDataByPointCodeAndTime(smallAirParam);
                // 把时间格式化一下
                Collections.reverse(lampBlackList);
                for (Map lampBlack : lampBlackList) {
                    Object curMT = lampBlack.get("MONITORTIME");
                    if (null != curMT && !"".equals(curMT)) {
                        lampBlack.put("MONITORTIME", String.valueOf(curMT).substring(0, 16));
                    }
                }
                monitorTimeList = CalendarUtil.getDatesBetweenTwoDate(startTime.substring(0, 16), endTime.substring(0, 16), "hour");
            } else if (DataTypeEnum.DAY_DATA.getValue().equals(dataType)) {
                lampBlackList = smallAirDao.findSmallDayDataByPointCodeAndTime(smallAirParam);
                Collections.reverse(lampBlackList);
                // 把时间格式化一下
                for (Map lampBlack : lampBlackList) {
                    Object curMT = lampBlack.get("MONITORTIME");
                    if (null != curMT && !"".equals(curMT)) {
                        lampBlack.put("MONITORTIME", String.valueOf(curMT).substring(0, 10));
                    }
                }
                monitorTimeList = CalendarUtil.getDatesBetweenTwoDate(startTime.substring(0, 10), endTime.substring(0, 10), "day");
            } else {
                lampBlackList = new ArrayList<>();
            }

            JSONArray concentrationBeforeArr = new JSONArray();
            JSONArray concentrationAfterArr = new JSONArray();
            boolean beforeFlag = false;
            boolean afterFlag = false;
            for (String monitorTime : monitorTimeList) {
                beforeFlag = false;
                afterFlag = false;
                for (Map lampBlack : lampBlackList) {

                    if (!monitorTime.equals(lampBlack.get("MONITORTIME"))) {
                        continue;
                    }
                    //情况：前探头
                    if ("1".equals(String.valueOf(lampBlack.get("ADDRESS")))) {
                        beforeFlag = true;
                        concentrationBeforeArr.add(null == lampBlack.get("CONCENTRATION") || "".equals(lampBlack.get("CONCENTRATION")) || "0".equals(lampBlack.get("CONCENTRATION").toString()) ? "-" : lampBlack.get("CONCENTRATION"));
                        //情况：后探头
                    } else if ("2".equals(String.valueOf(lampBlack.get("ADDRESS")))) {
                        afterFlag = true;
                        concentrationAfterArr.add(null == lampBlack.get("CONCENTRATION") || "".equals(lampBlack.get("CONCENTRATION")) || "0".equals(lampBlack.get("CONCENTRATION").toString()) ? "-" : lampBlack.get("CONCENTRATION"));
                    } else {
                    }

                }

                if (!beforeFlag) {
                    concentrationBeforeArr.add("-");
                }
                if (!afterFlag) {
                    concentrationAfterArr.add("-");
                }
            }
            JSONArray dataList = new JSONArray();
            dataList.add(concentrationBeforeArr);
            dataList.add(concentrationAfterArr);

            JSONArray lampBlackNameList = new JSONArray();
            lampBlackNameList.add("浓度1");
            lampBlackNameList.add("浓度2");

            detailInfoObject = new JSONObject();
            detailInfoObject.put("dataList", dataList);
            detailInfoObject.put("pollutionFactor", lampBlackNameList);
            detailInfoObject.put("time", monitorTimeList);
        }

        return detailInfoObject;
    }

    @SuppressWarnings("rawtypes")
    @Override
    public List<Map<String, Object>> findPollutionNewHourDataByPSCodeAndOutputCodes(List<String> siteCodeList,
                                                                                    List<String> outputCodeList) throws Exception {
        List<Map> psNewDataList = industryPollutionDao.findPollutionNewHourDataByPSCodeAndOutputCodes(siteCodeList,
            outputCodeList);

        List<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>();
        Map<String, Object> finalSiteData = null;
        // 按照公司，把相应污染物的平均值计算出来
        for (String siteCode : siteCodeList) {

            Object monitorTime = null;
            Double AvgStrength001 = 0.0;
            Double AvgStrength002 = 0.0;
            Double AvgStrength003 = 0.0;

            Double AvgZsStrength001 = 0.0;
            Double AvgZsStrength002 = 0.0;
            Double AvgZsStrength003 = 0.0;

            Double PFL001 = 0.0;
            Double PFL002 = 0.0;
            Double PFL003 = 0.0;

            Double AvgFlow = 0.0;
            Double CouFlow = 0.0;

            int index = 0;

            for (Map siteDataMap : psNewDataList) {
                Object siteCodeTemp = siteDataMap.get("PSCode");
                if (siteCode.equals(siteCodeTemp)) {
                    index = index + 1;

                    monitorTime = siteDataMap.get("MONITORTIME");
                    Object AvgStrength001Obj = siteDataMap.get("AvgStrength001");
                    AvgStrength001Obj = null == AvgStrength001Obj || "".equals(AvgStrength001Obj) ? "0.0"
                        : AvgStrength001Obj;
                    AvgStrength001 = AvgStrength001 + Double.valueOf(AvgStrength001Obj.toString());

                    Object AvgStrength002Obj = siteDataMap.get("AvgStrength002");
                    AvgStrength002Obj = null == AvgStrength002Obj || "".equals(AvgStrength002Obj) ? "0.0"
                        : AvgStrength002Obj;
                    AvgStrength002 = AvgStrength002 + Double.valueOf(AvgStrength002Obj.toString());

                    Object AvgStrength003Obj = siteDataMap.get("AvgStrength003");
                    AvgStrength003Obj = null == AvgStrength003Obj || "".equals(AvgStrength003Obj) ? "0.0"
                        : AvgStrength003Obj;
                    AvgStrength003 = AvgStrength003 + Double.valueOf(AvgStrength003Obj.toString());

                    Object AvgZsStrength001Obj = siteDataMap.get("AvgZsStrength001");
                    AvgZsStrength001Obj = null == AvgZsStrength001Obj || "".equals(AvgZsStrength001Obj) ? "0.0"
                        : AvgZsStrength001Obj;
                    AvgZsStrength001 = AvgZsStrength001 + Double.valueOf(AvgZsStrength001Obj.toString());

                    Object AvgZsStrength002Obj = siteDataMap.get("AvgZsStrength002");
                    AvgZsStrength002Obj = null == AvgZsStrength002Obj || "".equals(AvgZsStrength002Obj) ? "0.0"
                        : AvgZsStrength002Obj;
                    AvgZsStrength002 = AvgZsStrength002 + Double.valueOf(AvgZsStrength002Obj.toString());

                    Object AvgZsStrength003Obj = siteDataMap.get("AvgZsStrength003");
                    AvgZsStrength003Obj = null == AvgZsStrength003Obj || "".equals(AvgZsStrength003Obj) ? "0.0"
                        : AvgZsStrength003Obj;
                    AvgZsStrength003 = AvgZsStrength003 + Double.valueOf(AvgZsStrength003Obj.toString());

                    Object PFL001Obj = siteDataMap.get("PFL001");
                    PFL001Obj = null == PFL001Obj || "".equals(PFL001Obj) ? "0.0" : PFL001Obj;
                    PFL001 = PFL001 + Double.valueOf(PFL001Obj.toString());

                    Object PFL002Obj = siteDataMap.get("PFL002");
                    PFL002Obj = null == PFL002Obj || "".equals(PFL002Obj) ? "0.0" : PFL002Obj;
                    PFL002 = PFL002 + Double.valueOf(PFL002Obj.toString());

                    Object PFL003Obj = siteDataMap.get("PFL003");
                    PFL003Obj = null == PFL003Obj || "".equals(PFL003Obj) ? "0.0" : PFL003Obj;
                    PFL003 = PFL003 + Double.valueOf(PFL003Obj.toString());

                    Object AvgFlowObj = siteDataMap.get("AvgFlow");
                    AvgFlowObj = null == AvgFlowObj || "".equals(AvgFlowObj) ? "0.0" : AvgFlowObj;
                    AvgFlow = AvgFlow + Double.valueOf(AvgFlowObj.toString());

                    Object CouFlowObj = siteDataMap.get("CouFlow");
                    CouFlowObj = null == CouFlowObj || "".equals(CouFlowObj) ? "0.0" : CouFlowObj;
                    CouFlow = CouFlow + Double.valueOf(CouFlowObj.toString());
                } // end if
            }

            if (index > 0) {
                AvgStrength001 = AvgStrength001 / index;
                AvgStrength002 = AvgStrength002 / index;
                AvgStrength003 = AvgStrength003 / index;

                AvgZsStrength001 = AvgZsStrength001 / index;
                AvgZsStrength002 = AvgZsStrength002 / index;
                AvgZsStrength003 = AvgZsStrength003 / index;

                PFL001 = PFL001 / index;
                PFL002 = PFL002 / index;
                PFL003 = PFL003 / index;

                finalSiteData = new HashMap<String, Object>();
                finalSiteData.put("PSCode", siteCode);
                finalSiteData.put("MONITORTIME", monitorTime);
                finalSiteData.put("AvgStrength001", decimalformat.format(AvgStrength001));
                finalSiteData.put("AvgStrength002", decimalformat.format(AvgStrength002));
                finalSiteData.put("AvgStrength003", decimalformat.format(AvgStrength003));

                finalSiteData.put("AvgZsStrength001", decimalformat.format(AvgZsStrength001));
                finalSiteData.put("AvgZsStrength002", decimalformat.format(AvgZsStrength002));
                finalSiteData.put("AvgZsStrength003", decimalformat.format(AvgZsStrength003));

                finalSiteData.put("PFL001", decimalformat.format(PFL001));
                finalSiteData.put("PFL002", decimalformat.format(PFL002));
                finalSiteData.put("PFL003", decimalformat.format(PFL003));

                finalSiteData.put("AvgFlow", decimalformat.format(AvgFlow));
                finalSiteData.put("CouFlow", decimalformat.format(CouFlow));

                returnList.add(finalSiteData);
            }

        }

        return returnList;
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    public Map<String, Object> findLast12HourIndustyHourDataBySiteCodeForTrend(String siteCode,
                                                                               List<String> outputCodeList) throws Exception {

        List<Map> psList = industryPollutionDao.findLast12HourIndustryHourDataBySiteCode(siteCode, outputCodeList);

        List<String> monitorTimeList = new ArrayList<String>();

        String curTime = "";
        for (Map dataMap : psList) {
            Object monitorTimeObj = dataMap.get("MONITORTIME");
            if (null != monitorTimeObj && !"".equals(monitorTimeObj)) {
                dataMap.put("MONITORTIME", String.valueOf(monitorTimeObj).substring(0, 16));
            }

            monitorTimeObj = dataMap.get("MONITORTIME");

            if (!curTime.equals(monitorTimeObj)) {
                curTime = monitorTimeObj.toString();
                monitorTimeList.add(curTime);
            }
        }

        List<String> AvgStrength001List = new ArrayList<String>();
        List<String> AvgStrength002List = new ArrayList<String>();
        List<String> AvgStrength003List = new ArrayList<String>();

        for (String monitorTime : monitorTimeList) {
            Double AvgStrength001 = 0.0;
            Double AvgStrength002 = 0.0;
            Double AvgStrength003 = 0.0;

            int index = 0;
            for (Map siteDataMap : psList) {
                Object monitorTimeObj = siteDataMap.get("MONITORTIME");
                if (monitorTime.equals(monitorTimeObj)) {
                    index = index + 1;
                    Object AvgStrength001Obj = siteDataMap.get("AvgStrength001");
                    AvgStrength001Obj = null == AvgStrength001Obj || "".equals(AvgStrength001Obj) ? "0.0"
                        : AvgStrength001Obj;
                    AvgStrength001 = AvgStrength001 + Double.valueOf(AvgStrength001Obj.toString());

                    Object AvgStrength002Obj = siteDataMap.get("AvgStrength002");
                    AvgStrength002Obj = null == AvgStrength002Obj || "".equals(AvgStrength002Obj) ? "0.0"
                        : AvgStrength002Obj;
                    AvgStrength002 = AvgStrength002 + Double.valueOf(AvgStrength002Obj.toString());

                    Object AvgStrength003Obj = siteDataMap.get("AvgStrength003");
                    AvgStrength003Obj = null == AvgStrength003Obj || "".equals(AvgStrength003Obj) ? "0.0"
                        : AvgStrength003Obj;
                    AvgStrength003 = AvgStrength003 + Double.valueOf(AvgStrength003Obj.toString());

                }
            }

            if (index > 0) {
                AvgStrength001 = AvgStrength001 / index;
                AvgStrength002 = AvgStrength002 / index;
                AvgStrength003 = AvgStrength003 / index;
            }

            AvgStrength001List.add(decimalformat.format(AvgStrength001));
            AvgStrength002List.add(decimalformat.format(AvgStrength002));
            AvgStrength003List.add(decimalformat.format(AvgStrength003));

        }

        Map<String, Object> detailInfoObject = new HashMap<String, Object>();
        detailInfoObject.put("AvgStrength001", AvgStrength001List);
        detailInfoObject.put("AvgStrength002", AvgStrength002List);
        detailInfoObject.put("AvgStrength003", AvgStrength003List);
        detailInfoObject.put("time", monitorTimeList);

        return detailInfoObject;
    }

    @SuppressWarnings("rawtypes")
    @Override
    public List<RaiseDustEntity> findRaiseDustData(Map<String, Object> map) throws Exception {
        // 判断用户是否具有访问权限
        if (!UserSiteUtil.userIsNotContainSiteTypes("102")) {
            return new ArrayList<RaiseDustEntity>();
        }
        // String pointType = String.valueOf(map.get("pointType"));
        // 数据类型：小时数据 hourData；日数据 dayData；5分钟数据 minuiteData
        String dataType = String.valueOf(map.get("dataType"));
        // 开始时间
        String startTime = String.valueOf(map.get("startTime"));
        // 结束时间
        String endTime = String.valueOf(map.get("endTime"));

        Map<String, Object> region = UserUtil.getRegion();
        String regionCode = (String) region.get("regionCode");
        // String regionType = (String) region.get("regionType");

        // 查询城市和企业名称
        String searchRegionCode = (String) map.get("regionCode");
        String searchCompanyName = (String) map.get("companyName");
        String siteCodes = (String) map.get("siteCodes");
        RegionSiteParam paraSiteMap = new RegionSiteParam();
        paraSiteMap.setRegionCode(regionCode);
        if (null != searchRegionCode) {
            paraSiteMap.setRegionCode(searchRegionCode);
        }
        paraSiteMap.setSiteType("raiseDustSite");
        paraSiteMap.setCompanyName(searchCompanyName);
        if (null != siteCodes && !"".equals(siteCodes)) {
            List<String> siteCodeList = Arrays.asList(siteCodes.split(","));
            paraSiteMap.setSiteCodeList(siteCodeList);
        }
        // 点位编码列表
        List<Map<String, Object>> siteInfoList = regionSiteDao.findSiteInfoByRegionCodeAndSiteType(paraSiteMap);
        if (siteInfoList == null || siteInfoList.size() == 0) {
            return new ArrayList<RaiseDustEntity>();
        }

        List<String> pointCodeList = new ArrayList<String>();
        List<String> snCodeList = new ArrayList<String>();
        for (Map<String, Object> siteMap : siteInfoList) {
            pointCodeList.add(String.valueOf(siteMap.get("siteCode")));
            snCodeList.add(String.valueOf(siteMap.get("SNCode")));
        }
        // 查询参数 PO类
        PollutionParam pollutionParam = new PollutionParam();
        pollutionParam.setSNCodeList(snCodeList);
        List<Map> resultList = null;
        List<RaiseDustEntity> rdList = new ArrayList<>();
        if (DataTypeEnum.MINUTE_DATA.getValue().equals(dataType)) {
            // 分钟数据
            pollutionParam.setEndTime(endTime);
            pollutionParam.setStartTime(startTime);
            resultList = pollutionDao.findPollutionMinuteDataByTimeAndSNCodeList(pollutionParam);
            setList(resultList, rdList, siteInfoList);
        } else if (DataTypeEnum.HOUR_DATA.getValue().equals(dataType)) {
            // 小时数据
            pollutionParam.setEndTime(endTime);
            pollutionParam.setStartTime(startTime);
            resultList = pollutionDao.findPollutionHourDataByTimeAndSNCodeList(pollutionParam);

            setList(resultList, rdList, siteInfoList);
        } else if (DataTypeEnum.DAY_DATA.getValue().equals(dataType)) {
            // 日数据
            Calendar calendar = Calendar.getInstance();
            // 当前时间
            endTime = null == endTime || "".equals(endTime)
                ? DateTimeUtil.getFormatDateStr(calendar.getTime(), DamConstant.ALL_SDF_YMD_H) : endTime;
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            startTime = null == startTime || "".equals(startTime)
                ? DateTimeUtil.getFormatDateStr(calendar.getTime(), DamConstant.ALL_SDF_YMD_H) : startTime;

            // 开始时间
            pollutionParam.setStartTime(startTime);
            // 结束时间
            pollutionParam.setEndTime(endTime);

//			List<Map> dayMapList = smallAirDao.findSmallDayDataByPointCodeAndTime(airQualityParamsPO);
//			setList(dayMapList, rdList, siteInfoList);
            // 平台暂无日数据,返回空
            return rdList;
        }
        return rdList;
    }

    @SuppressWarnings("rawtypes")
    private void setList(List<Map> dayMapList, List<RaiseDustEntity> rdList, List<Map<String, Object>> siteInfoList) {
        RaiseDustEntity raiseDustEntity = null;
        int index = 0;
        for (Map dayData : dayMapList) {
            Map<String, Object> monitorData = (Map<String, Object>) dayData.get("MONITORDATA");
            raiseDustEntity = new RaiseDustEntity();
            raiseDustEntity.setIndex(++index);
            Object PM10Obj = monitorData.get("CTPM10");
            if (null == PM10Obj || "0".equals(String.valueOf(PM10Obj)) || "0.0".equals(String.valueOf(PM10Obj))) {
                raiseDustEntity.setPM10("-");
            } else {
                raiseDustEntity.setPM10(String.valueOf(PM10Obj));
            }
            Object PM25Obj = monitorData.get("CTPM25");
            if (null == PM25Obj || "0".equals(String.valueOf(PM25Obj)) || "0.0".equals(String.valueOf(PM25Obj))) {
                raiseDustEntity.setPM25("-");
            } else {
                raiseDustEntity.setPM25(String.valueOf(PM25Obj));
            }
            Object WDObj = monitorData.get("WD");
            if (null == WDObj || "0".equals(String.valueOf(WDObj)) || "0.0".equals(String.valueOf(WDObj))) {
                raiseDustEntity.setWindDirection("-");
            } else {
                raiseDustEntity.setWindDirection(WeatherUtil.changeAngleToWindDirection(String.valueOf(WDObj)));
            }
            Object WSObj = monitorData.get("WS");
            if (null == WSObj || "0".equals(String.valueOf(WSObj)) || "0.0".equals(String.valueOf(WSObj))) {
                raiseDustEntity.setWindSpeed("-");
            } else {
                raiseDustEntity.setWindSpeed(WeatherUtil.changeWindSpeedToPower(String.valueOf(WSObj)));
            }
            Object TEMPObj = monitorData.get("TEMP");
            if (null == TEMPObj || "0".equals(String.valueOf(TEMPObj)) || "0.0".equals(String.valueOf(TEMPObj))) {
                raiseDustEntity.setTemperature("-");
            } else {
                raiseDustEntity.setTemperature(String.valueOf(TEMPObj));
            }
            Object HUMObj = monitorData.get("HUM");
            if (null == HUMObj || "0".equals(String.valueOf(HUMObj)) || "0.0".equals(String.valueOf(HUMObj))) {
                raiseDustEntity.setHumidity("-");
            } else {
                raiseDustEntity.setHumidity(String.valueOf(HUMObj));
            }
            Object siteCodeObj = dayData.get("SNCODE");
            if (null != siteCodeObj) {
                for (Map siteInfo : siteInfoList) {
                    if (String.valueOf(siteCodeObj).equals(siteInfo.get("SNCode"))) {
                        raiseDustEntity.setSiteName(siteInfo.get("siteName").toString());
                        raiseDustEntity.setSiteCode(siteInfo.get("siteCode").toString());
                        raiseDustEntity.setRegionName(siteInfo.get("RegionName").toString());
                        break;
                    }
                }
            }
            Object monitorTimeObj = dayData.get("MONITORTIME");
            if (null != monitorTimeObj) {
                raiseDustEntity.setMonitorTime(String.valueOf(monitorTimeObj).substring(0, 16));
            }
            rdList.add(raiseDustEntity);
        }
    }

}
